import UIKit
import  SwiftEventBus

class SettingViewController: UIViewController {
    let scrollView = UIScrollView()
    var containerView: UIView!
    let vipView = UIView()
    var chatHistoryItemView:  UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "我的"
        self.view.backgroundColor = .white
        view.addSubview(scrollView)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-56)
        }
        containerView = UIView()
        scrollView.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
            make.width.equalTo(UIScreen.main.bounds.width)
        }
        
        if !StoreManager.isVip(){
                        
            vipView.isUserInteractionEnabled = true
            vipView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(tapVipView)))
            vipView.backgroundColor = UIColor( "#ff8960")
            vipView.layer.masksToBounds = true
            vipView.layer.cornerRadius = 8
            let gradientLayer = CAGradientLayer()
            gradientLayer.frame = self.view.bounds
            //设置渐变的主颜色
            gradientLayer.colors = [UIColor( "#ff679d").cgColor, UIColor("#ff8960").cgColor]
            gradientLayer.startPoint = CGPoint(x: 0, y: 0.5)
            gradientLayer.endPoint = CGPoint(x: 1, y: 0.5)
            vipView.layer.addSublayer(gradientLayer)
            
            containerView.addSubview(vipView)
            let vipTitle = UILabel()
            
            vipTitle.font = UIFont.boldSystemFont(ofSize: 18)
            vipTitle.textColor = .white
            vipView.addSubview(vipTitle)
            
            let vipDes = UILabel()
            
            vipDes.font = UIFont.boldSystemFont(ofSize: 12)
            vipDes.textColor = .white.withAlphaComponent(0.6)
            vipView.addSubview(vipDes)
            
            vipView.snp.makeConstraints { make in
                make.top.equalTo(containerView.snp.top).offset(24)
                make.left.equalTo(containerView).offset(16)
                make.right.equalTo(containerView).offset(-16)
                make.height.equalTo(60)
            }
            
            vipTitle.snp.makeConstraints { make in
                make.top.equalTo(vipView).offset(6)
                make.left.equalTo(vipView).offset(16)
            }
            vipDes.snp.makeConstraints { make in
                make.top.equalTo(vipTitle.snp.bottom).offset(6)
                make.left.equalTo(vipTitle)
            }
            
            vipTitle.text = "解锁所有高级功能"
            vipDes.text = "解锁所有功能,更快响应，更智能"
            
            let rightButton = UIButton(type: .custom)
            rightButton.addTarget(self, action: #selector(purchaseButtonTapped(_:)), for: .touchUpInside)
            rightButton.setTitle(" 立即开通 ", for: .normal)
            rightButton.layer.cornerRadius = 15
            rightButton.setTitleColor(.white.withAlphaComponent(0.9), for: .normal)
            rightButton.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            rightButton.backgroundColor = UIColor( "#712004").withAlphaComponent(0.5)
            
            
            vipView.addSubview(rightButton)
            rightButton.snp.makeConstraints { make in
                make.right.equalTo(vipView).offset(-16)
                make.centerY.equalTo(vipView)
                make.width.equalTo(76)
                make.height.equalTo(30)
            }
        }
        
        
        //chat
        var tap = UITapGestureRecognizer.init(target: self, action: #selector(tapChatHistory))
        chatHistoryItemView = getItemView("对话历史",tap)
        containerView.addSubview(chatHistoryItemView)
        chatHistoryItemView.snp.makeConstraints { make in
            if StoreManager.isVip(){
                make.top.equalTo(containerView.snp.top).offset(24)
            }else{
                make.top.equalTo(vipView.snp.bottom).offset(12)
            }
            
        }
        
        //completion
        tap = UITapGestureRecognizer.init(target: self, action: #selector(tapCompletionHistory))
        let completionItemView = getItemView("创作历史",tap)
        containerView.addSubview(completionItemView)
        completionItemView.snp.makeConstraints { make in
            make.top.equalTo(chatHistoryItemView.snp.bottom).offset(12)
        }
        
        
        //share
        tap = UITapGestureRecognizer.init(target: self, action: #selector(tapShare))
        let shareItemView = getItemView("分享好友",tap)
        containerView.addSubview(shareItemView)
        shareItemView.snp.makeConstraints { make in
            make.top.equalTo(completionItemView.snp.bottom).offset(12)
        }
        
        //suggest
        tap = UITapGestureRecognizer.init(target: self, action: #selector(tapSuggest))
        let suggestItemView = getItemView("问题反馈",tap)
        containerView.addSubview(suggestItemView)
        suggestItemView.snp.makeConstraints { make in
            make.top.equalTo(shareItemView.snp.bottom)
        }
        
        
        //隐私条款
        tap = UITapGestureRecognizer.init(target: self, action: #selector(tapYinsi))
        let yinsiItemView = getItemView("隐私条款",tap)
        containerView.addSubview(yinsiItemView)
        yinsiItemView.snp.makeConstraints { make in
            make.top.equalTo(suggestItemView.snp.bottom)
        }
        
        
        //用户协议
        tap = UITapGestureRecognizer.init(target: self, action: #selector(tapXieyi))
        let xieyiItemView = getItemView("用户协议",tap)
        containerView.addSubview(xieyiItemView)
        xieyiItemView.snp.makeConstraints { make in
            make.top.equalTo(yinsiItemView.snp.bottom)
        }
        
        
        
        containerView.snp.makeConstraints({ (make) in
            make.bottom.equalTo(xieyiItemView.snp.bottom)
            
        })
        

        
        SwiftEventBus.onMainThread(self, name: EVENT_NAME_REFRESH_VIP_STATE) { result in
            if StoreManager.isVip(){
                self.vipView.isHidden = true
                self.chatHistoryItemView.snp.makeConstraints { make in
                    make.top.equalTo(self.containerView.snp.top).offset(24)
                }
            }else{
                self.chatHistoryItemView.snp.makeConstraints { make in
                    make.top.equalTo(self.vipView.snp.bottom).offset(12)
                }
                   
                self.vipView.isHidden = false
            }
            
        }
        
    }
    @objc func tapVipView() {
        let detailViewController = VipViewController()
        detailViewController.fromPage = "设置"
        self.navigationController?.pushViewController(detailViewController, animated: true)
    }
    
    @objc func tapChatHistory() {
        var historyVC = HistoryViewController()
        historyVC.pageTitle = "对话历史"
        historyVC.type = 1
        self.navigationController?.pushViewController(historyVC, animated: true)
    }
    
    @objc func tapCompletionHistory() {
        var historyVC = HistoryViewController()
        historyVC.pageTitle = "创作历史"
        historyVC.type = 2
        self.navigationController?.pushViewController(historyVC, animated: true)
    }
    @objc func tapShare() {
        DispatchQueue.main.async {
            let activityController = UIActivityViewController(activityItems: [URL(string: "https://apps.apple.com/cn/app/id")!], applicationActivities: nil)
            activityController.modalPresentationStyle = .fullScreen
            activityController.completionWithItemsHandler = {
                (type, flag, array, error) -> Void in
                
            }
            self.present(activityController, animated: true, completion: nil)
        }
    }
    
    @objc func tapSuggest() {
        let detailViewController = SuggestViewController()
        
        self.navigationController?.pushViewController(detailViewController, animated: true)
    }
    
    @objc func tapYinsi() {
        let urlString = "https://docs.qq.com/doc/DZVZubHhRZWlwbXJM"
        if let url = URL(string: urlString) {
            //根据iOS系统版本，分别处理
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:],
                                          completionHandler: {
                    (success) in
                })
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }
    
    @objc func tapXieyi() {
        let urlString = "https://docs.qq.com/doc/DZVZubHhRZWlwbXJM"
        if let url = URL(string: urlString) {
            //根据iOS系统版本，分别处理
            if #available(iOS 10, *) {
                UIApplication.shared.open(url, options: [:],
                                          completionHandler: {
                    (success) in
                })
            } else {
                UIApplication.shared.openURL(url)
            }
        }
    }
    
    func getItemView(_ title: String,_ tap: UITapGestureRecognizer )->UIView{
        var itemView = UIView()
        containerView.addSubview(itemView)
        itemView.snp.makeConstraints { make in
            make.left.equalTo(containerView).offset(16)
            make.right.equalTo(containerView).offset(-16)
            make.height.equalTo(56)
            make.width.equalTo(UIScreen.main.bounds.width-32)
        }
        
        var titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textColor = .black
        titleLabel.font = UIFont.boldSystemFont(ofSize: 16)
        itemView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(itemView)
            make.left.equalTo(itemView)
        }
        
        
        let image = UIImage(systemName: "chevron.forward")?.withRenderingMode(.alwaysTemplate)
        let imageView = UIImageView(image: image)
        imageView.tintColor = .gray
        
        itemView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.right.equalTo(itemView)
            make.centerY.equalTo(itemView)
            make.width.equalTo(12)
            make.height.equalTo(18)
        }
        
        
        itemView.isUserInteractionEnabled = true
        itemView.addGestureRecognizer(tap)
        return  itemView
    }
    
    override var wx_titleTextAttributes: [NSAttributedString.Key : Any]? {
        return [NSAttributedString.Key.foregroundColor: UIColor.black]
    }
    
    @objc private func purchaseButtonTapped(_ sender: Any) {
        let detailViewController = VipViewController()
        detailViewController.fromPage = "设置"
        self.navigationController?.pushViewController(detailViewController, animated: true)
    }
}

